package com.koro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.koro.dto.FindOrdersDTO;
import com.koro.dto.SaveOrdersDTO;
import com.koro.entity.Orders;
import com.koro.mapper.OrdersMapper;
import com.koro.service.OrdersService;
import com.koro.vo.FindOrdersVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public Integer save(SaveOrdersDTO dto) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(dto,orders);
        if(!"".equals(dto.getId()) || dto.getId() == 0) {
            ordersMapper.insert(orders);
        } else {
            ordersMapper.updateById(orders);
        }
        return orders.getId();
    }

    @Override
    public FindOrdersVO findById(FindOrdersDTO dto) {
        Orders orders = null;
        if(!"".equals(dto.getId())) {
            orders = ordersMapper.selectById(dto.getId());
        } else {
            QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("order_num",dto.getKeyworks());
            orders = ordersMapper.selectOne(queryWrapper);
        }
        FindOrdersVO vo = new FindOrdersVO();
        BeanUtils.copyProperties(orders,vo);
        return vo;
    }
}
